<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/8
 * Time: 17:26
 */

namespace App\Rep;


use App\Models\ModelNumber;
use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\ProductJobType;
use App\Models\ProductModelNumber;
use Illuminate\Support\Facades\DB;

class ModelNumberRep
{

    /**
     * 获取型号列表
     * @param $params
     * @return mixed
     */
    public function getModelNumberListRep($params){
        if(isset($params['product_id']) && !empty($params['product_id'])){
            $id_arr = ProductModelNumber::where('product_id',$params['product_id'])->pluck('model_number_id')->toArray();
            if(!empty($params['field_list'])){
                $select_arr = explode(',',$params['field_list']);
            }else{
                $select_arr = "*";
            }
            $model = ModelNumber::whereIn('id',$id_arr);

            if(isset($params['is_dropdown']) && !empty($params['is_dropdown'])){
                if(isset($params['file'])){
                    if($params['file'] == 1){
                        array_push($select_arr,'file_id');
                        $model->with('file');
                    }
                }

                $list = $model->select($select_arr)->paginate($params['per_page']??1000);
            }else{
                if(isset($params['file'])){
                    if($params['file'] == 1){
                        array_push($select_arr,'file_id');
                        $model->with('file');
                    }
                }

                $list = $model->with('product')->select($select_arr)->paginate($params['per_page']??1000);
            }
        }else{
            $list = ModelNumber::Search($params)->with('product')->where('is_del',2)->with('file')->paginate($params['per_page']??1000);
        }

        return $list;
    }

    /**
     * 添加型号
     * @param $params
     * @return ModelNumber|array
     */
     public function addModelNumberRep($params){
         DB::beginTransaction();
         try {
             $obj = ModelNumber::updateOrCreate(
                 ['model_number'=>$params['model_number']],
                     ['model_number'=>$params['model_number'],'file_id'=>$params['file_id']]
             );
             foreach($params['product_ids'] as $k=>$v){
                 ProductModelNumber::updateOrCreate(['product_id'=>$v,'model_number_id'=>$obj->id],['product_id'=>$v,'model_number_id'=>$obj->id]);
             }
             DB::commit();
             return ['code'=>0,'data'=>[],'msg'=>''];
         }catch(\Exception $e){
             DB::rollBack();
             return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
         }
     }
    /**
     * 产品关联型号
     * @param $params
     * @return array
     */
    public function createModelNumberRep($params){
        DB::beginTransaction();
        try {
            if (isset($params['model_number_id']) && !empty($params['model_number_id'])) {
                $model = ModelNumber::where('id', $params['model_number_id'])->first();
            } else {
                $model = new ModelNumber();
            }
            //p($model->toArray());
            $model->model_number = $params['model_number'];
            if (isset($params['file_id']) && !empty($params['file_id'])) {
                $model->file_id = $params['file_id'];
            }
            $model->save();
            $product_id = $params['product_id'];
            ProductModelNumber::updateOrCreate(
                ['model_number_id'=>$model->id,'product_id'=>$product_id],
                ['model_number_id'=>$model->id,'product_id'=>$product_id]
            );
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>''];
        }catch(\Exception $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
        }
    }

    /**
     * 删除型号
     * @param $params
     */
    public function delModelNumberRep($params){
        $model = ModelNumber::where('id',$params['id'])->first();
        $model->is_del = 1;
        $model->save();
//        $where = ['model_number_id'=>$params['id'],'product_id'=>$params['product_id']];
//        ProductModelNumber::where($where)->delete();
        ProductModelNumber::where('model_number_id',$params['id'])->delete();
        ModelNumberSpecification::where('model_number_id',$params['id'])->delete();
        return $model;
    }

    public function delProductModelnumberRep($params){
        $where = ['model_number_id'=>$params['model_number_id'],'product_id'=>$params['product_id']];
        ProductModelNumber::where($where)->delete();
        return true;
    }


    /**
     * 批量添加型号
     * @param $params
     */
    public function batchAddModelNumberRep($params){
        $array = $params['model_number_ids'];
        foreach($array as $k=>$v){
            $where = ['model_number_id'=>$v,'product_id'=>$params['product_id']];
            ProductModelNumber::updateOrCreate($where,$where);
        }
        return ['code'=>0,'data'=>[],'msg'=>''];
    }

}
